/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package cn.ac.iie.di.wx.mongoservice;

import cn.ac.iie.di.tools.MusExtract;
import cn.ac.iie.di.wx.utils.DateUtil;
import static cn.ac.iie.di.wx.utils.GenerateMD5Util.string2MD5;
import cn.ac.iie.di.wx.vo.ReadCntMqVo;
import cn.ac.iie.di.wx.vo.ReadCntVo;
import java.net.MalformedURLException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import org.apache.log4j.Logger;

/**
 *
 * @author Administrator
 */
public class ReadCntDocinfoMongoThread implements Runnable {

    public MongoHelper mongoHelper;
    public LinkedBlockingQueue<ReadCntVo> blockingQueue;
    public LinkedBlockingQueue<ReadCntMqVo> readCntMqBlockingQueue;
    public static Logger logger = Logger.getLogger(ReadCntDocinfoMongoThread.class.getName());

    public ReadCntDocinfoMongoThread(MongoHelper mongoHelper, LinkedBlockingQueue<ReadCntVo> blockingQueue, LinkedBlockingQueue<ReadCntMqVo> readCntMqBlockingQueue) {
        this.mongoHelper = mongoHelper;
        this.blockingQueue = blockingQueue;
        this.readCntMqBlockingQueue = readCntMqBlockingQueue;
    }

    public MongoHelper getMongoHelper() {
        return mongoHelper;
    }

    public void setMongoHelper(MongoHelper mongoHelper) {
        this.mongoHelper = mongoHelper;
    }

    public LinkedBlockingQueue<ReadCntVo> getBlockingQueue() {
        return blockingQueue;
    }

    public void setBlockingQueue(LinkedBlockingQueue<ReadCntVo> blockingQueue) {
        this.blockingQueue = blockingQueue;
    }

    @Override
    public void run() {
        while (true) {
            try {
                ArrayList<String> collection = new ArrayList<>();
                ReadCntVo vo = blockingQueue.poll(1000, TimeUnit.SECONDS);
                if (vo != null) {
                    collection.add(string2MD5(vo.getUrl()));
                    HashMap<String, Object> rnt = mongoHelper.find("urlmd5", collection);
                    for (Map.Entry<String, Object> entrySet : rnt.entrySet()) {
                        entrySet.setValue(Long.parseLong(vo.getNrd()));
                    }
                    if (rnt.size() > 0) {
                        mongoHelper.update("_id", "nrd", rnt);
                        //回写docinfo队列的消息队列
                        readCntMqBlockingQueue.add(new ReadCntMqVo("docinfo", "_spec", "40", DateUtil.getTimeStamp(), MusExtract.getMus(vo.getUrl()), vo.getNrd(), vo.getUrl()));
                        logger.info("update readCount to Docinfo mongoDB");
                    }
                }

            } catch (InterruptedException ex) {
                logger.error(ex.getMessage(), ex);
            } catch (ParseException ex) {
                logger.error(ex.getMessage(), ex);
            } catch (MalformedURLException ex) {
                logger.error(ex.getMessage(), ex);
            }
        }
    }

}
